<?php require_once('Connections/locamedicadmin.php'); ?>

<?php require_once('functions.php'); ?>


<?php

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_commandes = 20;
$pageNum_commandes = 0;
if (isset($_GET['pageNum_commandes'])) {
  $pageNum_commandes = $_GET['pageNum_commandes'];
}
$startRow_commandes = $pageNum_commandes * $maxRows_commandes;

$codigo = $_GET['codigo'];

mysql_select_db($database_locamedicadmin, $locamedicadmin);
$query_commandes = sprintf("select * from csre_commande_detail cd left join csre_product p on p.product_id = cd.product_id where codigo = '%s' ", $codigo);
//$query_limit_commandes = sprintf("%s LIMIT %d, %d", $query_commandes, $startRow_commandes, $maxRows_commandes);
$commandes = mysql_query($query_commandes, $locamedicadmin) or die(mysql_error());
$row_commandes = mysql_fetch_assoc($commandes);

if (isset($_GET['totalRows_commandes'])) {
  $totalRows_commandes = $_GET['totalRows_commandes'];
} else {
  $all_commandes = mysql_query($query_commandes);
  $totalRows_commandes = mysql_num_rows($all_commandes);
}
$totalPages_commandes = ceil($totalRows_commandes/$maxRows_commandes)-1;

$maxRows_subtotal = 10;
$pageNum_subtotal = 0;
if (isset($_GET['pageNum_subtotal'])) {
  $pageNum_subtotal = $_GET['pageNum_subtotal'];
}
$startRow_subtotal = $pageNum_subtotal * $maxRows_subtotal;

mysql_select_db($database_locamedicadmin, $locamedicadmin);
$query_subtotal = "SELECT * FROM csre_commande_subtotals where id_commande = '" . $codigo . "'";
$query_limit_subtotal = sprintf("%s LIMIT %d, %d", $query_subtotal, $startRow_subtotal, $maxRows_subtotal);
$subtotal = mysql_query($query_limit_subtotal, $locamedicadmin) or die(mysql_error());
$row_subtotal = mysql_fetch_assoc($subtotal);

if (isset($_GET['totalRows_subtotal'])) {
  $totalRows_subtotal = $_GET['totalRows_subtotal'];
} else {
  $all_subtotal = mysql_query($query_subtotal);
  $totalRows_subtotal = mysql_num_rows($all_subtotal);
}
$totalPages_subtotal = ceil($totalRows_subtotal/$maxRows_subtotal)-1;

mysql_select_db($database_locamedicadmin, $locamedicadmin);
$query_facturacion = sprintf("SELECT * FROM csre_commande_addr_facturation WHERE codigo = '%s'", $codigo);
$facturacion = mysql_query($query_facturacion, $locamedicadmin) or die(mysql_error());
$row_facturacion = mysql_fetch_assoc($facturacion);
$totalRows_facturacion = mysql_num_rows($facturacion);

mysql_select_db($database_locamedicadmin, $locamedicadmin);
$query_livraison = "SELECT * FROM csre_commande_addr_livraison WHERE codigo = '" . $codigo . "'";
$livraison = mysql_query($query_livraison, $locamedicadmin) or die(mysql_error());
$row_livraison = mysql_fetch_assoc($livraison);
$totalRows_livraison = mysql_num_rows($livraison);


mysql_select_db($database_locamedicadmin, $locamedicadmin);
$query_commanda = sprintf("SELECT * FROM csre_commande WHERE codigo = '%s'", $codigo);
$commanda = mysql_query($query_commanda, $locamedicadmin) or die(mysql_error());
$row_commanda = mysql_fetch_assoc($commanda);
$totalRows_commanda = mysql_num_rows($commanda);

$queryString_commandes = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_commandes") == false && 
        stristr($param, "totalRows_commandes") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_commandes = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_commandes = sprintf("&totalRows_commandes=%d%s", $totalRows_commandes, $queryString_commandes);
?>


	<link href="locamedicadmin.css" rel="stylesheet" type="text/css" />
	
		<?php if ($totalRows_commandes > 0) { ?>
		<table cellpadding="5" cellspacing="0">
		  <thead>
	      <td>Product Code</td>
		    <td>Product Name</td>			
			<td>Taxable</td>
			<td>Service</td>
			<td>Price</td>
			<td>Amount</td>
			<td>Total</td>
		 </thead>
		  <?php 
		  $total = 0;
		  do { 
		  
		  ?>
		  
			<tr>
			  <td><?php echo $row_commandes['csre_number']; ?></td>
			  <td><?php echo $row_commandes['name_FR']; ?></td>
			  <td><?php if ($row_commandes['taxable']) { echo "OUI"; } else { echo "NON"; }?></td>
			  <td>
			  
			  <?php 
			  
			  	mysql_select_db($database_locamedicadmin, $locamedicadmin);
				$query_service = "SELECT description FROM csre_serv_types WHERE type_id = " . $row_commandes['service'];
				$service = mysql_query($query_service, $locamedicadmin) or die(mysql_error());
				$row_service = mysql_fetch_assoc($service);
				echo $row_service['description'];
			  
			  ?>
			  
			  </td>
			  <td>$ <?php echo $row_commandes['price']; ?></td>
			  <td><?php echo $row_commandes['amount']; ?></td>	
  			  <td>$ <?php echo ($row_commandes['price'] * $row_commandes['amount']); ?></td>	
			</tr>
			<?php } while ($row_commandes = mysql_fetch_assoc($commandes)); ?>

		</table>
		
		<?php }  ?>
		<table>
			<tr>
				<td colspan="2">
				<table cellpadding="5" cellspacing="0">
				  <thead>
				  	<td colspan="2">Totals</td>
				  </thead>
				  <tr>
                    <td>Non Tax Produit</td>
                    <td>$ <?php echo $row_subtotal['non_tax_produit']; ?></td>
				  </tr>
				  </tr>
                    <td>Tax Produit</td>
                      <td>$ <?php echo $row_subtotal['tax_produit']; ?></td>
				  </tr>
				  </tr>
                    <td>Frais de Service</td>
					<td>$ <?php echo $row_subtotal['frais_de_service']; ?></td>
				  </tr>
				  </tr>
                    <td>TPS</td>
	                <td>$ <?php echo $row_subtotal['tps']; ?></td>
				  </tr>
				  </tr>
                    <td>TVQ</td>
                    <td>$ <?php echo $row_subtotal['tvq']; ?></td>
				  </tr>
				  </tr>
                    <td>Total</td>
                    <td>$ <?php echo $row_subtotal['total']; ?></td>
                  </tr>
                </table>
				</td>
				<td colspan="2">
				<table cellpadding="5" cellspacing="0">
				<thead>
					<td colspan="2">Adresse de facturation</td>
				</thead>
                  <tr>
                    <td>Contact</td>
					<td><?php echo $row_facturacion['contact']; ?></td>
				  </tr>
				  <tr>
                    <td>Adresse</td>
                    <td><?php echo $row_facturacion['addr']; ?></td>
					</tr>
				  <tr>
                    <td>Ville</td>
                    <td><?php echo $row_facturacion['ville']; ?></td>
					</tr>
				  <tr>
                    <td>Province</td>
                    <td><?php echo $row_facturacion['province']; ?></td>
					</tr>
				  <tr>
                    <td>Code postal</td>
                    <td><?php echo $row_facturacion['code_postal']; ?></td>
					</tr>
				  <tr>
                    <td>Phone</td>
                    <td><?php echo $row_facturacion['phone']; ?></td>
                  </tr>
                </table></td>
				<td colspan="2">
				<table cellpadding="5" cellspacing="0">
				<thead>
					<td colspan="2">Adresse de livraison</td>
				</thead>
                  <tr>
                    <td>Contact</td>
					<td><?php echo $row_livraison['contact']; ?></td>
				  </tr>
				  <tr>
                    <td>Adresse</td>
                    <td><?php echo $row_livraison['addr']; ?></td>
					</tr>
				  <tr>
                    <td>Ville</td>
                    <td><?php echo $row_livraison['ville']; ?></td>
					</tr>
				  <tr>
                    <td>Province</td>
                    <td><?php echo $row_livraison['province']; ?></td>
					</tr>
				  <tr>
                    <td>Code postal</td>
                    <td><?php echo $row_livraison['code_postal']; ?></td>
					</tr>
				  <tr>
                    <td>Phone</td>
                    <td><?php echo $row_livraison['phone']; ?></td>
                  </tr>
                </table></td>
			</tr>
			</table>
			<table cellpadding="5" cellspacing="0">
				<thead>
					<td>Conditions</td>
				</thead>
				<tr>
					<td><?php echo $row_commanda['conditions']; ?> </td>
				</tr>
			</table>
			<table cellpadding="5" cellspacing="0">
				<thead>
					<td>Information pour le fournisseur</td>
				</thead>
				<tr>
					<td><?php echo $row_commanda['inf_four']; ?> </td>
				</tr>
			</table>
